package com.atguigu.gulimall.product.web;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.Catelog2Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author ct_start
 * @create 2022 -02 -18
 **/
@Controller
public class IndexController {

    @Autowired
    CategoryService categoryService; // 注入分类的dao 查询所有的一级分类




//    首页映射 发送请求 "/" 或者 "/index.html" 都是跳转到首页
    @GetMapping({"/","/index.html"})
    public String IndexPage(Model model){

//        1. 先查询出所有的一级分类
       List<CategoryEntity> categoryEntity =categoryService.getCategoryLevle1();
       model.addAttribute("categorys",categoryEntity);


        return "index";
    }

//    index/catalog.json 这里是发送这个请求,返回封装好的JSon数据
    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String, List<Catelog2Vo>> getCatalogJson(){


        Map<String, List<Catelog2Vo>> catalogJson = categoryService.getCatalogJson();
        return catalogJson;
    }
}
